﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BizzReports;
using System.Windows.Forms;
using System.Drawing;

namespace OutboxStatsReport
{
    class OutboxStatsReport : BizzReportTemplate
    {
        
        private OutboxStatsReportCommand cmd;
        ToolStripComboBox outBoxCombo;
        DataGridView dgAttributes;

        public OutboxStatsReport(IBizzCommand sourceCmd)
            : base(sourceCmd)
        {
            cmd = (OutboxStatsReportCommand)sourceCmd;
            initOutboxStatsReport();
        }

        private void initOutboxStatsReport()
        {
            //Datagrid vista previa de atributos
            dgAttributes = new DataGridView();
            dgAttributes.Dock = DockStyle.Fill;
            dgAttributes.AllowUserToAddRows = false;
            dgAttributes.AllowUserToDeleteRows = false;
            dgAttributes.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            dgAttributes.BackgroundColor = System.Drawing.SystemColors.Window;
            dgAttributes.BorderStyle = System.Windows.Forms.BorderStyle.None;
            dgAttributes.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.SingleHorizontal;
            dgAttributes.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dgAttributes.Dock = System.Windows.Forms.DockStyle.Fill;
            dgAttributes.GridColor = System.Drawing.SystemColors.Window;
            dgAttributes.ReadOnly = true;
            dgAttributes.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            dgAttributes.RowHeadersVisible = false;
            tableLayoutPanel1.Controls.Add(dgAttributes);
            tableLayoutPanel1.SetRow(dgAttributes, 3);
            
            //
            //ComboBox Bandejas de salida
            toolStrip1.Items.Add(new ToolStripSeparator());
            outBoxCombo = new ToolStripComboBox();
            outBoxCombo.AutoSize = true;
            toolStrip1.Items.Add(outBoxCombo);
            outBoxCombo.SelectedIndexChanged += new EventHandler(outBoxCombo_SelectedIndexChanged);
            outBoxCombo.Items.AddRange(cmd.getOutboxes());
            //Fechas
            fromDate.Value = cmd.fromDate;
            toDate.Value = cmd.toDate;
            //
            if (outBoxCombo.Items.Count > 0)
                outBoxCombo.SelectedItem = outBoxCombo.Items[0];
            //
            

            //Grilla principal
            dgContent.RowHeadersVisible = false;
            dgContent.AutoGenerateColumns = true;
            Update();
            dgContent.DataSource = cmd.reportData;
            dgContent.Columns[0].Visible = false;
            dgContent.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgContent.AlternatingRowsDefaultCellStyle.BackColor = Color.Gainsboro;
            dgContent.SelectionChanged += new EventHandler(dgContent_SelectionChanged);
            //
            
            


        }

        void dgContent_SelectionChanged(object sender, EventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            if (dgv != null && dgv.SelectedRows.Count > 0)
            {
                DataGridViewRow row = dgv.SelectedRows[0];
                if (row != null)
                {
                    cmd.selectedMessageId = row.Cells[0].Value.ToString();
                    dgAttributes.DataSource = cmd.getMessageAttributes();
                    
                }
            }
        }

        void outBoxCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmd.selectedOutbox = outBoxCombo.SelectedItem.ToString();
            update();
        }

        public override void tsRefresh_Click(object sender, EventArgs e)
        {
            update();
        }

        private void update()
        {
            cmd.fromDate = fromDate.Value;
            cmd.toDate = toDate.Value;
            cmd.Update();
            dgContent.DataSource = cmd.reportData;
        }

    }
}
